#include<iostream>
#include<vector>

using namespace std;

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<int> row(n), cal(n);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                row[i] = max(row[i], grid[i][j]);
                cal[j] = max(cal[j], grid[i][j]);
            }
        int res = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                int t = min(row[i], cal[j]);
                res += t - grid[i][j];
            }
        return res;
    }
};